<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="950" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px"
			label-position="left">
			<el-form-item label="服务商名称" prop="provider_id">
				<el-select v-model="form.provider_id" filterable style="width: 100%" clearable
					:disabled="mode == 'edit'">
					<el-option v-for="item in changeStatus" :key="item.provider_id" :label="item.provider_name"
						:value="item.provider_id" />
				</el-select>
			</el-form-item>
			<el-form-item label="服务商商品ID" prop="provider_product_id">
				<el-input v-model="form.provider_product_id" placeholder="请输入服务商商品ID" clearable></el-input>
			</el-form-item>
			<el-form-item label="商品名称" prop="product_name">
				<el-input v-model="form.product_name" placeholder="商品名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="商品背景图" prop="product_pic">
				<sc-upload v-model="form.product_pic" :completeUrl="form.product_pic_url" :limit="1" title="商品背景图">
				</sc-upload>
				<div class="el-upload__tip text-red">
					大小1m以内，支持png、jpg、jpeg格式，尺寸建议 670*248
				</div>
			</el-form-item>
			<el-row>

				<el-form-item label="服务人次时间" prop="service_month">
					<el-col :span="50">
						<el-form-item label="" prop="service_month">
							<el-col :span="50">
								<el-input v-model="form.service_month" placeholder="服务时间" clearable>
									<template #append>月
									</template>
								</el-input>
							</el-col>
						</el-form-item>
					</el-col>
					<el-col :span="50">
						<el-form-item label="" prop="service_user_num">
							<el-col :span="50">
								<el-input v-model="form.service_user_num" placeholder="服务人数" clearable>
									<template #append>人
									</template>
								</el-input>
							</el-col>
						</el-form-item>
					</el-col>
				</el-form-item>
			</el-row>
			<el-form-item label="是否绿通服务" prop="is_green">
				<el-switch v-model="form.is_green" active-value="1" inactive-value="0" />
			</el-form-item>
			<el-row v-if="form.is_green == 1">
				<el-form-item label="服务等待期" prop="wait_day">
					<el-col :span="50">
						<el-form-item label="" prop="wait_day">
							<el-col :span="50">
								<el-input v-model="form.wait_day" placeholder="请输入门诊绿通服务等待期" clearable>
									<template #append>天</template>
								</el-input>
							</el-col>
						</el-form-item>
					</el-col>
					<el-col :span="50">
						<el-form-item label="" prop="service_times">
							<el-col :span="50">
								<el-input v-model="form.service_times" placeholder="请输入门诊绿通次数" clearable>
									<template #append>次数</template>
								</el-input>
							</el-col>
						</el-form-item>
					</el-col>
				</el-form-item>
			</el-row>
			<el-form-item label="采购数量" prop="purchase_qty">
				<el-input v-model="form.purchase_qty" placeholder="采购数量" clearable></el-input>
			</el-form-item>
			<el-form-item label="采购价" prop="purchase_price">
				<el-input v-model="form.purchase_price" placeholder="采购价" clearable>
					<template #append>元
					</template>
				</el-input>
			</el-form-item>
			<el-form-item label="销售价" prop="sale_price">
				<el-input v-model="form.sale_price" placeholder="销售价" clearable>
					<template #append>元
					</template>
				</el-input>
			</el-form-item>
			<el-form-item label="有效期" prop="valid_date">
				<el-date-picker v-model="form.valid_date" type="datetimerange" start-placeholder="请选择开始时间"
					end-placeholder="请选择结束时间" format="YYYY-MM-DD HH:mm:ss" date-format="YYYY/MM/DD ddd"
					time-format="A hh:mm:ss" />
			</el-form-item>
			<el-form-item label="上架状态" prop="status">
				<el-select v-model="form.status" filterable style="width: 100%" clearable>
					<el-option v-for="item in statusList" :key="item.status" :label="item.status_name"
						:value="item.status" />
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新建商品',
				edit: '编辑商品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				provider_id: "",
				provider_product_id: "",
				product_name: "",
				product_pic: "",
				product_pic_url: "",
				service_month: "",
				service_user_num: "",
				is_green: 0,
				wait_day: "",
				service_times: "",
				purchase_qty: "",
				purchase_price: "",
				sale_price: "",
				valid_date: "",
				status: "",
			},
			//验证规则
			rules: {
				provider_id: [
					{ required: true, message: '请选择服务商', trigger: 'change' }
				],
				provider_product_id: [
					{ required: true, message: '请输入服务商商品ID' }
				],
				product_name: [
					{ required: true, message: '请输入商品名称' }
				],
				product_pic: [
					{ required: true, message: '请上传商品背景图' }
				],
				service_month: [
					{ required: true, message: '请输入服务人次时间' },
				],
				service_user_num: [
					{ required: true, message: '请输入服务人数' },
				],
				is_green: [
					{ required: true, message: '请选择是否绿通服务' },
				],
				wait_day: [
					{ required: true, message: '请输入门诊绿通服务等待期' },
				],
				service_times: [
					{ required: true, message: '请输入门诊绿通次数' },
				],
				purchase_qty: [
					{ required: true, message: '请输入正确的采购数量，数量必须是大于0的整数' },
				],
				purchase_price: [
					{ required: true, message: '请输入正确的采购价，价格必须大于0', }
				],
				sale_price: [
					{ required: true, message: '请输入正确的销售价，价格必须大于0', }
				],
				valid_date: [
					{ required: true, message: '请选择有效期' }
				],
				status: [
					{ required: true, message: '请选择上下架状态', trigger: 'change' },
				],

			},
			//所需数据选项
			statusList: [
				{ status: 1, status_name: "上架" },
				{ status: 2, status_name: "下架" },
			],
			changeStatus: [],
		}
	},
	mounted() {
		this.getProviderList()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		getProviderList() {
			this.$API['service_provider.service_provider'].getProviderList.get().then((res) => {
				if (res.code == 0) {
					this.changeStatus = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", { type: 'error' })
			})
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.$API['service_provider.service_provider'].productStore.post(this.form)
						.then((res) => {
							if (res.code == 0) {
								this.$emit('success', this.form, this.mode)
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", { type: 'error' })
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", { type: 'error' })
						})
						.finally(() => {
							this.isSaveing = false;
						});
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.product_id = data.product_id
			this.form.provider_id = data.provider_id
			this.form.provider_product_id = data.provider_product_id
			this.form.product_name = data.product_name
			this.form.product_pic = data.product_pic
			this.form.product_pic_url = data.product_pic_url
			this.form.service_month = data.service_month
			this.form.service_user_num = data.service_user_num
			this.form.is_green = data.is_green
			this.form.wait_day = data.wait_day
			this.form.service_times = data.service_times
			this.form.purchase_qty = data.purchase_qty
			this.form.purchase_price = data.purchase_price
			this.form.sale_price = data.sale_price
			this.form.status = parseInt(data.status)
			this.form.valid_date = data.valid_date
			// this.form.canadd_patient_user_num = data.canadd_patient_user_num
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style></style>
